package com.sskj.tibi.ui.fragment;


import android.graphics.Bitmap;
import android.support.design.widget.BottomSheetDialog;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hey.lib.HeySpinner;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.ImageUtil;
import com.sskj.lib.util.QRCodeUtil;
import com.sskj.tibi.R;
import com.sskj.tibi.R2;
import com.sskj.tibi.bean.RechargeBean;
import com.sskj.tibi.presenter.RechargeFragmentPresenter;

import butterknife.BindView;


@Route(path = RConfig.TIBI_FRAGMENT_RECHARGE)//充币
public class RechargeFragment extends BaseFragment<RechargeFragmentPresenter> {

    @BindView(R2.id.ivQR)
    ImageView ivQR;
    @BindView(R2.id.tvAddress)
    TextView tvAddress;
    @BindView(R2.id.btCopy)
    Button btCopy;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    @BindView(R2.id.tvSave)
    TextView tvSave;
    @BindView(R2.id.heySpinner)
    HeySpinner heySpinner;
    private BottomSheetDialog bottomSheet;

    @Override
    protected int getLayoutId() {
        return R.layout.tibi_fragment_recharge;
    }

    @Override
    public RechargeFragmentPresenter getPresenter() {
        return new RechargeFragmentPresenter();
    }

    @Override
    protected void initView() {
        heySpinner.setText("OMNI");
        ClickUtil.click(heySpinner, () -> {
            bottomSheet = BottomSheetUtil.getBottomSheet(getActivity(), null, (recyclerView, i, view) -> {
                bottomSheet.dismiss();
                heySpinner.setText(i == 0 ? "OMNI" : "ERC20");
                mPresenter.getData(i == 0 ? "2" : "1");
            }, "OMNI", "ERC20");
            bottomSheet.show();
        });
    }

    @Override
    protected void initData() {
        mPresenter.getData("2");
    }

    public void updateUI(RechargeBean data) {
//        ImageUtil.setImage(data.getCodeUrlImg(), ivQR);
        QRCodeUtil.createImage(data.getAddr(), 400, new QRCodeUtil.OnEncodeQRCodeCallback() {
            @Override
            public void onAnalyzeSuccess(Bitmap bitmap) {
                ivQR.setImageBitmap(bitmap);
            }

            @Override
            public void onAnalyzeFailed() {

            }
        });

        ivQR.setOnLongClickListener(v -> {
            ImgUtil.saveImageToGallery(getActivity(), ivQR);
            return true;
        });
        ClickUtil.click(tvSave, () -> {
            ImgUtil.saveImageToGallery(getActivity(), ivQR);

        });
        tvAddress.setText(data.getAddr());
        ClickUtil.click(btCopy, () -> {
            CopyUtil.copy(data.getAddr());
        });

    }


}
